<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Class Mmenu extends MY_Model
{
    public function __construct()
    {
        parent::__construct();
    }
    
    public function getNamecate()
    {
        return $this->db->query("SELECT * FROM menu ORDER BY order_by ASC")->result_object();
    }
    
    public function getDB()
    {
        return $this->db->query("SELECT * FROM menu ORDER BY order_by ASC")->result_object();
    }
    
    public function getDBID($id)
    {
        return $this->db->query("SELECT * FROM menu WHERE id = '".$id."' ORDER BY order_by ASC")->row_object();
    }
    
    public function getMenu($id)
    {
        $var = $this->db->query('SELECT * FROM menu WHERE id = '.$id)->row_object();
        $exp_tags = explode(',',$var->tags);
        $tags = '';
        foreach($exp_tags AS $el)
        {
            $tags .= '"'.$el.'",';
        }
        $var->tags_show = substr($tags, 0, -1);
        return $var;
    }
    
    
    public function sortsaveDB($id_list)
    {
        foreach($id_list AS $key=>$val)
        {
            $this->db->update('categories', array('order_by' => $key), "id = '".$val."'");   
        }
        return 200;    
    }

    public function getTypeLink($id)
    {
        $data = $this->db->query('SELECT * FROM type_categories ORDER BY id ASC')->result_object();
        $html  = '';
        foreach($data AS $el=>$le)
        {
            if($id == $le->id)
            {
                $selected = 'selected="selected"';
            }else{
                $selected = '';
            }
            $html .= '<option value="'.$le->id.'" '.$selected.'>'.$le->title.'</option>';
        }
        return $html;
    }

    public function getalllinkDB()
    {
        $id = $this->getParamInt('id');
        $type = $this->getParamInt('type');
        $editmenu = $this->getParamInt('editmenu');
        if($type == 2)
        {
            $where = '';
        }else if($type == 3){
            $where = 'WHERE type_cate = '.$editmenu;
        }else{
            $where = 'WHERE type_cate = '.$id;
        }
        $data = $this->db->query('SELECT * FROM link_all '.$where.' ORDER BY id,vsc ASC')->result_object();
        $html  = '<select class="form-control" id="standard-list1" name="link_all">
                          <option value="">Chọn link liên kết</option>';
        foreach($data AS $el=>$le)
        {
            if($id == $le->id)
            {
                $selected = 'selected="selected"';
            }else{
                $selected = '';
            }
            $html .= '<option value="'.$le->id.'" '.$selected.'>'.$le->title.'</option>';
        }
        $html .= '</select>';
        return $html;
    }
    

    public function editDb($id,$img,$baseurl)
    {
        $id_links = $this->getParamString('link_all');
        $link_menu = $this->db->query('SELECT * FROM link_all WHERE id ='.$id_links)->row_object();
        $data = array(
                    'title'         => $this->getParamString('nameMenu'),
                    'link_menu'     => $link_menu->link,
                    'id_type'       => $this->getParamString('typeLink'),
                    'id_link'       => $id_links,
                    'parent'       => $this->getParamInt('menu')
                    );
        
        $this->db->update('menu', $data,"id = ".$id."");
        return 200;
    }
    
    public function addDb($img,$baseurl)
    {   
        $id_links = $this->getParamString('link_all');
        $link_menu = $this->db->query('SELECT * FROM link_all WHERE id ='.$id_links)->row_object();
        $data = array(
                    'title'         => $this->getParamString('nameMenu'),
                    'link_menu'     => $link_menu->link,
                    'id_type'       => $this->getParamString('typeLink'),
                    'id_link'       => $id_links,
                    'parent'       => $this->getParamInt('menu')
                    );
        $this->db->insert('menu', $data);
        return 200;
    }
    
    
    public function deleteDB()
    {
        $id = $this->getParamInt('id');
        $this->db->delete('menu',"id = '".$id."'");
        return 200;
    }
    
    
    public function getParent($selected)
    {
        $html = '';
    	$gRs = $this->db->query("SELECT id AS gid,title AS gtitle,parent AS gparent FROM menu WHERE parent = 0 ORDER BY order_by ASC")->result_array();
        if($gRs)
        {
            $child = '|--';
            foreach ($gRs As $gel)
        	{
            	   if($selected == $gel['gid'])
                   {
                        $selecteds = 'selected="selected"';
                   }else{
                        $selecteds = '';
                   }
    	    		$html .= ' <option value="' . $gel['gid'] . '"'.$selecteds.'>'.$gel['gtitle'].'</option>';
                    $html .= $this->subMenuGroupotp($selected, $gel['gid'], '');
        	}
        }
        return $html;
    }
    
    private function subMenuGroupotp($selected, $id, $child)
    {
        $child = $child .'|--';
        $htmls = '';
        $gGs = $this->db->query("SELECT id AS sGid,parent AS sGparent, title AS sGtitle FROM menu WHERE parent = '".$id."' ORDER BY id ASC")->result_array();
        foreach ($gGs As $key=>$val)
		{
		      if($selected == $val['sGid'])
               {
                    $selecteds = 'selected="selected"';
               }else{
                    $selecteds = '';
               }
                $htmls .= '<option value="' . $val['sGid'] . '" '.$selecteds;
                $htmls .= ($selected == $val['sGid']) ? 'selected="selected"' : '';
				$htmls .= '>' . $child .' '. $val['sGtitle'] . '</option>
                ';
                $htmls .= $this->subMenuGroupotp($selected, $val['sGid'], $child);
		}
        return $htmls;
    }
    
    public function getMenuDB($baseurl)
    {
        $html = '';
        $data = $this->db->query('SELECT c.title AS ctitle,c.id AS cid,c.link_menu AS clink_menu FROM menu AS c WHERE parent = 0 ORDER BY c.id,order_by ASC')->result_array();
        if($data)
        {
            $i = 1;
            foreach($data AS $le=>$el)
            {
                $child = '|---- ';
                $html .= '<tr>
                            <td><span class="xedit"><center>'.$i.'</center></span></td>
                            <td class="hidden-xs"><span class="xedit"><strong>'.$el['ctitle'].'</strong></span></td>
                            <td class="hidden-xs"><span class="xedit">
                                <a target="_blank" href="'.$baseurl.$el['clink_menu'].'">
                                    '.$el['ctitle'].'
                                </a>
                            </span></td>
                            <td class="hidden-xs text-center"><div class="btn-group">
                                <button type="button" class="btn btn-danger btn-gradient dropdown-toggle" data-toggle="dropdown"> <span class="glyphicons glyphicons-cogwheel"></span> </button>
                                <ul class="dropdown-menu checkbox-persist pull-right text-left" role="menu">
                                  <li><a href="'.$baseurl.'manage/edit-menu.html?id='.$el['cid'].'"><i class="fa"></i> Sửa </a></li>
                                  <li><a href="'.$baseurl.'manage/delete-menu.html?id='.$el['cid'].'"><i class="fa"></i> Xóa </a></li>
                                </ul>
                              </div></td>
                          </tr>';
                $i++;
                $html .= $this->subgetMenu($el['cid'],$baseurl,$child,$i);
                
            }
        }
        
        return $html;
    }
    
    private function subgetMenu($id,$baseurl,$child,$i)
    {
        $html = ''; @$child = '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'.$child;
        $data = $this->db->query('SELECT c.title AS ctitle,c.id AS cid,c.link_menu AS clink_menu FROM menu AS c WHERE parent = "'.$id.'" ORDER BY c.id,order_by ASC')->result_array();
        if($data)
        {
            foreach($data AS $le=>$el)
            {
                $html .= '<tr>
                            <td><span class="xedit">&nbsp;</span></td>
                            <td class="hidden-xs"><span class="xedit">'.$child.$el['ctitle'].'</span></td>
                            <td class="hidden-xs"><span class="xedit">
                                <a target="_blank" href="'.$baseurl.$el['clink_menu'].'">
                                    '.$el['ctitle'].'
                                </a>
                            </span></td>
                            <td class="hidden-xs text-center"><div class="btn-group">
                                <button type="button" class="btn btn-danger btn-gradient dropdown-toggle" data-toggle="dropdown"> <span class="glyphicons glyphicons-cogwheel"></span> </button>
                                <ul class="dropdown-menu checkbox-persist pull-right text-left" role="menu">
                                  <li><a href="'.$baseurl.'manage/edit-menu.html?id='.$el['cid'].'"><i class="fa"></i> Sửa </a></li>
                                  <li><a href="'.$baseurl.'manage/delete-menu.html?id='.$el['cid'].'"><i class="fa"></i> Xóa </a></li>
                                </ul>
                              </div></td>
                          </tr>';
                $html .= $this->subgetMenu($el['cid'],$baseurl,$child,$i);
                
            }
        }
        return $html;
    }
    
    
}